import Vue from 'vue'
import Vuex from 'vuex'
// import { pullAllBy } from 'lodash'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    loginInfo: JSON.parse(uni.getStorageSync('loginInfo') || '{}'),
    latLog: JSON.parse(uni.getStorageSync('latLog') || '{}'),
    // 商城公司列表
    shopComList: JSON.parse(uni.getStorageSync('shopComList') || '{}'),
    shopComIndex: JSON.parse(uni.getStorageSync('shopComIndex') || 'null'),
    // 品牌信息
    brandInfo: { brand: 'ZEEKR', brand_id: 52 },
    // 模板ID
    tmplIds: {
      testDrivea: 'nVAPpopWJF4TE1bOyqnQ7AcWZXVvv1S8vPo5M2QP3m0', // 试驾预约模板ID
      // maintain: 'ZqK-5ly7jPhD0KleBQLoF2Znl9fDABEl5o6Zb8rHsac'  // 保养预约模板ID
    },
    // 车型
    carSeris: [],
    // 服务中心
    centers: [],
    // 服务中心列表，用于券中心
    centersList: [],
    // 地区id
    regionId: -1,
    // 商店经纬度
    shopLatLon: {},
    // 汽车详情页信息
    carDetailInfo: {},
    // 汽车详情车型
    veStyle: [],
    // 汽车详情图列表
    // imgInfo: [],
    // 车型的详情参数
    carsParamAll: {},
    // 车型参数列表
    carParaList: [],
    // 集团信息
    groupInfo: [],
    // 车型列表
    carModelDataList: [],
    // 立即买
    goodsOrder: JSON.parse(uni.getStorageSync('goodsOrder') || '[]'),
    // 收货地址
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    // 优惠券列表
    couponByPhone: [],
  },
  mutations: {
    saveCarSeris(state, data) {
      state.carSeris = data
    },
    // 服务中心列表，用于券中心
    saveCentersList(state, data) {
      state.centersList = data
    },
    // 服务中心
    saveCenters(state, data) {
      console.log('服务中心 :>> ', data);
      state.centers = data
    },
    saveLoginInfo(state, v) {
      state.loginInfo = v
      this.commit('setLoginInfo')
    },
    setLoginInfo(state) {
      uni.setStorageSync('loginInfo', JSON.stringify(state.loginInfo))
    },
    // 坐标
    saveLatLog(state, data) {
      state.latLog = data
    },
    // 保存汽车详情页信息
    saveCarDetail(state, v) {
      state.carDetailInfo = v
    },
    // 保存汽车详情参数信息
    saveCarVeStyle(state, v) {
      state.veStyle = v
    },
    // 汽车详情图列表
    // saveCarCarDetailImgInfo(state, v) {
    //   state.veStyle = v
    // },
    // 车型的详情参数
    saveCarsParamAll(state, v) {
      state.carsParamAll = v
    },
    // 车型参数列表
    saveCarParaList(state, v) {
      state.carParaList = v
    },
    // 保存集团信息
    saveGroupInfo(state, v) {
      state.groupInfo = v
    },
    //  车型列表
    saveCarModelDataList(state, v) {
      state.carModelDataList = v
    },
    // 立即买
    saveGoodsOrder(state, v) {
      let indes = state.goodsOrder.findIndex(
        item => item.goods_id === v.goods_id
      )
      if (indes < 0) {
        state.goodsOrder.push(v)
      } else {
        if (v.detailPage) return false
        state.goodsOrder[indes].num++
      }
      this.commit('saveGoodsOrderStorage')
    },
    //  立即买,更改商品数量
    changeGoodsNub(state, v) {
      state.goodsOrder.forEach((item, index) => {
        if (item.goods_id === v.goods_id) {
          if (v.num > 0) {
            item.num = v.num
          } else {
            item.num = 0
          }
        }
      })
      this.commit('saveGoodsOrderStorage')
      // 下面更新goods列表
      if (data.num === 0) {
        setTimeout(() => {
          this.commit('spliceGoodsList')
        }, 50)
      }
    },
    spliceGoodsList(state) {
      state.goodsOrder = state.goodsOrder.filter(item => item.num != 0)
      this.commit('saveGoodsOrderStorage')
    },
    // 去掉当前公司没有的商品
    deteleGoodsList(state, v) {
      if (v.length) {
        let arr3 = v.map(item => item.goods_id)
        state.goodsOrder = state.goodsOrder.filter(x => arr3.indexOf(x.goods_id) < 0)
        this.commit('saveGoodsOrderStorage')
      }
    },
    // 清空
    emptyShoppCart(state) {
      state.goodsOrder = []
      this.commit('saveGoodsOrderStorage')
    },
    //  立即买,更改商品状态
    updateGoodsState(state, v) {
      for (let j = 0; j < state.goodsOrder.length; j++) {
        if (state.goodsOrder[j].goods_id === v.goods_id) {
          state.goodsOrder[j].select = !v.select
          this.commit('saveGoodsOrderStorage')
        }
      }
    },
    // 立即买，成功后，清空选择的
    updateGoodsOrderPay(state) {
      state.goodsOrder = state.goodsOrder.filter(item => item.state != 1)
      this.commit('saveGoodsOrderStorage')
    },
    // 立即买，保存到缓存
    saveGoodsOrderStorage(state) {
      uni.setStorageSync('goodsOrder', JSON.stringify(state.goodsOrder))
    },
    // 更新收货地址
    updateAddress(state, v) {
      // 判断是否删除地址
      if (v.del) {
        if (state.address.id === v.id) {
          state.address = {}
          this.commit('saveAddressStorage')
        }
        return false
      }
      state.address = v
      this.commit('saveAddressStorage')
    },
    // 修改收货地址
    amendAddress(state, v) {
      if (state.address.id === v.id) {
        state.address = v
        this.commit('saveAddressStorage')
      }
    },
    // 收货地址
    saveAddressStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    // 优惠券列表
    saveCouponByPhone(state, v) {
      state.couponByPhone = v
    },
    // 商城公司列表
    saveShopComList(state, v) {
      state.shopComList = v
      uni.setStorageSync('shopComList', JSON.stringify(v))
    },
    saveComIndex(state, v) {
      state.shopComIndex = v
      uni.setStorageSync('shopComIndex', JSON.stringify(v))
    }
  },
  getters: {
    // 选中商品总价格
    totalPrice(state) {
      let order = state.goodsOrder
      if (!order.length) return 0.00
      let total = 0
      for (let i = 0; i < order.length; i++) {
        if (order[i].select) {
          total =
            (Math.round(total * 10 * 10) + Math.round(Number(order[i].price) * 10 * 10 * order[i].num)) / 100
        }
      }
      return total
    },
    // 当前公司信息
    currComInfo(state) {
      // console.log('state.shopComIndex :>> ', state.shopComIndex);
      // console.log('state.shopComList :>> ', state.shopComList);
      if (!state.shopComList.length) return {}
      let index = state.shopComIndex
      if (!index || index == 'null' || index == 'undefined') {
        return state.shopComList[0]
        // return state.shopComList.find(v => v.id == 1)
      } else {
        // console.log('index', index)
        return state.shopComList.find(v => v.id == index)
      }
    },
  },
})
